package com.saver.modules.sys.controller;

import com.saver.common.db.JdbcTool;
import com.saver.common.utils.PageUtils;
import com.saver.common.utils.Query;
import com.saver.common.utils.R;
import com.saver.config.ServerConfig;
import com.saver.modules.sys.dao.SysParamDao;
import com.saver.modules.sys.entity.SysParamEntity;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/sys/param")
public class SysParamController {

    @Autowired
    private SysParamDao sysParamDao;

    @Autowired
    private JdbcTool jdbcTool;

    @Autowired
    private ServerConfig serverConfig;

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> map) {
        Query<Object> objectQuery = new Query<>(map);
        String sql = "select * from sys_param sp";
        Object q = map.get("q");
        if (q != null && !"".equals(q)) {
            sql = sql + " where sp.mark like '%" + q + "%'";
        }
        PageUtils pageUtils = jdbcTool.queryPage(sql, objectQuery);
        return R.ok().put("page", pageUtils);
    }

    @GetMapping("/getConfig")
    public String getConfig(@RequestParam("name") String name) {
        return sysParamDao.findByParamName(name).getParamValue();
    }

    @PostMapping("/setConfig")
    public R setConfig(@RequestBody SysParamEntity sysParamEntity) {
        SysParamEntity byParamName = sysParamDao.findByParamName(sysParamEntity.getParamName());
        byParamName.setParamValue(sysParamEntity.getParamValue());
        sysParamDao.save(byParamName);
        return R.ok();
    }
}
